package licode.unisop.client.feign.impl;

import licode.unisop.client.api.auth.AuthTokenShim;
import licode.unisop.client.api.auth.SopResourceQuery;
import licode.unisop.client.feign.SopUserAuthFeign;
import licode.unisop.client.info.*;
import licode.unisop.client.utils.MethodCallUtil;
import licode.unisop.client.vo.SopUrlMatch;
import licode.unisop.client.vo.SopUserAssets;
import licode.unisop.client.vo.SopUserType;
import licode.unisop.client.vo.SopVerifyCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 实现对资源的远程调用
 *
 * @author licode
 */
@Service
public class SopResourceQueryFeignImpl implements SopResourceQuery {
    @Resource
    private AuthTokenShim authTokenShim;

    @Resource
    private SopUserAuthFeign sopUserAuthFeign;

    private String getModalToken(String platformId) {
        return authTokenShim.modalToken(platformId);
    }

    @Override
    public SopUserType getUserType(SopPlatUserFind userFind) {
        return null;
    }

    @Override
    public SopUserAssets getUserAssets(SopTokenInfo tokenInfo) {
        return MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.getUserAssets(tokenInfo.getRealmId(),
                        getModalToken(tokenInfo.getToken()),
                        tokenInfo.getPlatformId(),
                        tokenInfo.getToken()));
    }

    @Override
    public SopUrlMatch matchUrl(SopUrlInfo urlInfo) {
        return MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.matchUrl(
                        urlInfo.getInfo().getRealmId(),
                        getModalToken(urlInfo.getInfo().getToken()),
                        urlInfo.getInfo().getPlatformId(),
                        urlInfo.getInfo().getToken(),
                        urlInfo.getUrl()));
    }

    @Override
    public SopUrlMatch ssoMatchUrl(SopUrlInfo urlInfo) {
        return MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.ssoMatchUrl(
                        urlInfo.getInfo().getRealmId(),
                        getModalToken(urlInfo.getInfo().getToken()),
                        urlInfo.getInfo().getPlatformId(),
                        urlInfo.getInfo().getToken(),
                        urlInfo.getUrl()));
    }

    @Override
    public SopVerifyCode hasRole(SopRoleInfo roleInfo) {
        return SopVerifyCode.build(MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.hasRole(
                        roleInfo.getInfo().getRealmId(),
                        getModalToken(roleInfo.getInfo().getToken()),
                        roleInfo.getInfo().getPlatformId(),
                        roleInfo.getInfo().getToken(),
                        roleInfo.getAction())));
    }

    @Override
    public SopVerifyCode hasPermit(SopPermitInfo permitInfo) {
        return SopVerifyCode.build(MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.hasPermit(
                        permitInfo.getInfo().getRealmId(),
                        getModalToken(permitInfo.getInfo().getToken()),
                        permitInfo.getInfo().getPlatformId(),
                        permitInfo.getInfo().getToken(),
                        permitInfo.getAction())));
    }

    @Override
    public SopVerifyCode hasAuthority(SopAuthorityIn info) {
        return SopVerifyCode.build(MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.hasAuthority(
                        info.getInfo().getRealmId(),
                        getModalToken(info.getInfo().getToken()),
                        info.getInfo().getPlatformId(),
                        info.getInfo().getToken(),
                        info.getAction())));
    }
}
